﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class LOGIN : Form
    {
        public string userName;
        public LOGIN()
        {
            InitializeComponent();
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            if(textBoxUser.Text == "" && textBoxPass.Text == "")
            {
                errorProvider1.SetError(textBoxUser, "Username Not Null");
                errorProvider2.SetError(textBoxPass, "Password Not Null");   
            }
            else
            {
                errorProvider1.Clear();
                errorProvider2.Clear();
                if (textBoxUser.Text == "")
                {
                    errorProvider1.SetError(textBoxUser, "Username Not Null");
                }
                else
                {
                    if (textBoxPass.Text == "")
                    {
                        errorProvider2.SetError(textBoxPass, "Password Not Null");
                    }
                    else
                    {
                        // khoi tao doi tuong de doc file config.ini
                        Config file = new Config(Application.StartupPath + "\\Config.ini");
                        String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                        SqlConnection conn = new SqlConnection(connectionstring);
                        string sql = "select * from NHANVIEN where IDnhanvien = @IDnhanvien";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        conn.Open();
                        cmd.Parameters.AddWithValue("@IDnhanvien", textBoxUser.Text);
                        SqlDataReader reader = cmd.ExecuteReader();
                        if (reader.Read() == true)
                        {
                            if ((string)reader["matkhau"] == textBoxPass.Text)
                            {
                                reader.Close();
                                this.DialogResult = DialogResult.OK;
                                this.userName = textBoxUser.Text;
                                this.Close();
                            }
                            else
                            {
                                reader.Close();
                                errorProvider2.SetError(textBoxPass, "Eror Pass");
                            }
                        }
                        else
                        {
                            reader.Close();
                            errorProvider1.SetError(textBoxUser, "Eror Username");
                        }
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                } 
            }   
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void textBoxPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonAccept_Click(sender, e);
            }
        }
    }
}
